
import random
from utils import *
import pygame.mixer
pygame.init()
ball_sound = pygame.mixer.Sound('../sounds/ball.mp3')

class Ball(pygame.sprite.Sprite):
    def __init__(self, *keys):  # 创建球
        super().__init__()
        self.timeSec = 0
        w, h, xs, ys = keys[0]
        self.w = w
        self.h = h
        self.xs = xs
        self.ys = ys
        x = random.randint(0, WIDTH - self.w)
        y = random.randint(0, HEIGHT - self.h)
        self.rect = pygame.Rect(x, y, self.w * 2, self.h * 2)

    def update(self, screen, *args):

        # 根据设置的速度进行运动
        self.rect.x += self.xs
        self.rect.y += self.ys
        # 当遇到墙的时候进行反弹
        if self.rect.x > WIDTH - self.w or self.rect.x < 0:
            ball_sound.play()
            self.xs = -self.xs
        elif self.rect.y > HEIGHT - self.h or self.rect.y < 0:
            ball_sound.play()
            self.ys = -self.ys
        if self.timeSec <= args[0]:
            pygame.draw.rect(screen, (100, 149, 237), [self.rect.x, self.rect.y, self.rect.w, self.rect.h], 2)
        pygame.draw.circle(screen, (255, 0, 0), [self.rect.center[0], self.rect.center[1]], self.w)

    def timerAdd(self):
        self.timeSec += 1
        return self.timeSec
    def __del__(self):  # 销毁的时候
        pass